package com.erpanderp.wam.tm.model.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.tm.model.entity.Skill;
import com.erpanderp.wam.tm.model.entity.SkillRating;
import com.erpanderp.wam.util.HibernateUtil;

public class SkillRatingDAO {
	
	 public SkillRating createSkillRatings(SkillRating skillRating){
			try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.save(skillRating);
			session.getTransaction().commit();
			return skillRating;
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
		}
	 
	 public boolean createSkillRatings(List<SkillRating> skillRatings){		
			try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);

			for(SkillRating skillRating : skillRatings){
				session.save(skillRating);	
			}
			session.getTransaction().commit();
			return true;
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}				
		}
	
	 public List<SkillRating> getSkillRatings(String rrfCode,Long recruitmentProfileId,String interviewLevel){
			List<SkillRating> skillRatings = new ArrayList<SkillRating>();			
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from SkillRating sr where sr.recruitmentRequestCode = :rrfCode and sr.recruitmentProfile= :recruitmentProfileId and sr.interviewLevel= :interviewLevel";
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setString("rrfCode", rrfCode).setLong("recruitmentProfileId",recruitmentProfileId).setString("interviewLevel",interviewLevel);			
				skillRatings =(List<SkillRating>) query.list();
				session.getTransaction().commit();
				return skillRatings;	
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
	}
	 
	public boolean isInterviewLevelFirstOrSecondLevel(String rrfCode,Long rrfProfileId,String interviewLevel) {
	boolean isFirstLevelOrSecondLevel = true;
	try {
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		session.getTransaction().begin();
		String queryString = "from SkillRating sr where sr.recruitmentRequestCode=:rrfCode and sr.recruitmentProfile.recruitmentProfileId =:rrfProfileId and sr.interviewLevel =:interviewLevel ";
		Query query = session.createQuery(queryString).setString(
				"rrfCode", rrfCode).setLong("rrfProfileId", rrfProfileId).setString("interviewLevel", interviewLevel);
			query.setCacheable(false);

		List tempList = query.list();

		if (tempList == null || tempList.size() == 0) {
			isFirstLevelOrSecondLevel = false;
		}
		
		session.close();
		return isFirstLevelOrSecondLevel;
		} catch (Exception e) {
			e.printStackTrace();
			return isFirstLevelOrSecondLevel;
		}
     }
	
	public List<String> getCommonSkills(){ 
		List<String> commonSkills = new ArrayList<String>();
		List<Skill> pSkills = new ArrayList<Skill>();
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Skill s where s.technology.id is null";			
			Query query = session.createQuery(queryString);
			query.setCacheable(false);			
			pSkills =(List<Skill>) query.list();
			for(Skill primarySkill : pSkills){
				commonSkills.add(primarySkill.getSkill());
			}
			session.getTransaction().commit();
			return commonSkills;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	 
	 public boolean saveSkills(List<Skill> skillList){		
			try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);

			for(Skill skill : skillList){
				session.saveOrUpdate(skill);	
			}
			session.getTransaction().commit();
			return true;
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}				
		}
	 
	 public static void main(String a[]){
		 System.out.println(new SkillRatingDAO().getCommonSkills()); 	
	 }

}
